//
//  FMDiantaiTableViewCell.m
//  PsychologyFM
//
//  Created by lanou on 16/1/12.
//  Copyright © 2016年 ZhangLuDu. All rights reserved.
//

#import "FMDiantaiTableViewCell.h"
#import "FMAntorDescViewController.h"

@implementation FMDiantaiTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumInteritemSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        layout.itemSize = CGSizeMake((kScreenWidth - 50) / 4, (kScreenWidth - 50) / 4 + 30);
        _diantaiCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth / 4 + 40) collectionViewLayout:layout];
        _diantaiCollectionView.scrollEnabled = NO;
        
        //  设置代理和数据源
        _diantaiCollectionView.delegate = self;
        _diantaiCollectionView.dataSource = self;
        //  注册cell
        [_diantaiCollectionView registerClass:[FMDiantaiCollectionViewCell class] forCellWithReuseIdentifier:@"FMDiantaiCollectionViewCell"];
        
        //  collectionView背景颜色是黑色,所以必须给一个背景色,不然无法看到你的控件
        _diantaiCollectionView.backgroundColor = FMWhite;
        
        [self.contentView addSubview:_diantaiCollectionView];
        
    }
    return self;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FMDiantaiCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FMDiantaiCollectionViewCell" forIndexPath:indexPath];
    if (_modelArray.count == 0) {
        return cell;
    }else
    {
        NSLog(@"%@",_modelArray);
    FMDiantaiModel *model = _modelArray[indexPath.row];
    cell.model = model;
    //cell.backgroundColor = FMRed;
    return cell;
    }
}

//  item点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    FMAntorDescViewController *user_detailVC = [[FMAntorDescViewController alloc]init];
    FMDiantaiModel *model = _modelArray[indexPath.row];
    user_detailVC.idStr = model.id;
    [_homeVC.navigationController pushViewController:user_detailVC animated:YES];

}

- (void)setModelArray:(NSArray *)modelArray
{
    _modelArray = modelArray;
    if (modelArray.count != 0) {
        [_diantaiCollectionView reloadData];
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
